package com.woniuxy.portal.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.portal.model.entity.Book;
import com.woniuxy.portal.model.entity.BookType;
import com.woniuxy.portal.service.BookService;
import com.woniuxy.portal.service.impl.BookServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author yellow Docter
 * @date 2024-10-15
 * @desc 蜗牛创想：www.woniuxy.com
 */
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;




    /**
     *   springmvc 的参数的获取
     *   1、http:localhost:8080/book/top?pageNum=1&pageSize=10
     *       前端: axios  get请求发送数据  url?参数=值&参数=值
     *       参数：pageNum=1&pageSize=10
     *       后端:
     *            直接获取:Integer pageNum, Integer pageSize
     *            注解方式:@RequestParam("pageNum") Integer pageNum,
     *            封装成对象获取: Page page
     *
     *   2、http:localhost:8080/book/top?1/10
     *         后端:  @PathVariable 直接获取 路劲参数
     *
     *   3、http:localhost:8080/book/top
     *       请求载荷: {"pageNum":1,pageSize:10}
     *       后端: @RequestBody Page page  如果是json格式的载荷 (@RequestBody)
     */
    @PostMapping("/top")
    public ResponseEntity<Page<Book>> findTopSales(@RequestBody Map<String,Integer> params){

        Integer pageNum = params.get("pageNum");
        Integer pageSize = params.get("pageSize");

        //调用service 分页查询
        Page<Book> topSales = bookService.findTopSales(pageNum, pageSize);

        return ResponseEntity.ok(topSales);
    }


    @PostMapping("/findByType")
    public ResponseEntity<Page<Book>> findByType(@RequestBody Map<String,Integer> params){

        Integer pageNum = params.get("pageNum");
        Integer pageSize = params.get("pageSize");
        Integer typeId = params.get("typeId");

        //调用service 分页查询
        Page<Book> bookPageByType = bookService.findByType(pageNum, pageSize, typeId);

        return ResponseEntity.ok(bookPageByType);
    }

    @GetMapping("/findById/{id}")
    public ResponseEntity<Book> findById(@PathVariable("id") Long id){
        return ResponseEntity.ok(bookService.findById(id));
    }

}
